//
//  RadarAnimationView.m
//  blueTooth
//
//  Created by macvivi on 2018/7/19.
//  Copyright © 2018年 macvivi. All rights reserved.
//

static const float timeinterval = 2.0;

#import <QuartzCore/QuartzCore.h>
#import "RadarAnimationView.h"
#import "UIView+Frame.h"
#import "NSTimer+WeakTimer.h"

@interface RadarAnimationView ()
{
    //定时器
//    NSTimer *_timer;
}
//按钮图片
@property (nonatomic,strong)UIButton *iamgeBtn;

@end

@implementation RadarAnimationView

- (void)drawRect:(CGRect)rect {
    
    self.alpha = 1.0;
    //创建定时器
    
    [self setUp];
  
    if ( _timer) {
        
    }else{
//        NSLog(@"创建定时器");
//        _timer = [NSTimer scheduledTimerWithTimeInterval:timeinterval target:self selector:@selector(setUp) userInfo:nil repeats:YES];
        //破除循环引用
       _timer = [NSTimer scheduledWeakTimerWithTimeInterval:timeinterval target:self selector:@selector(setUp) userInfo:nil repeats:YES];
        
    }
    
    
  
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
//        _iamgeBtn = [[UIButton alloc]    initWithFrame:self.bounds];
        _iamgeBtn = [UIButton new];
        _iamgeBtn.width = 32;
        _iamgeBtn.height = 32;
//        _iamgeBtn.backgroundColor = [UIColor redColor];
        _iamgeBtn.alpha = 1;
        [self addSubview:_iamgeBtn];
        _iamgeBtn.userInteractionEnabled = NO;
//        self.backgroundColor = [UIColor yellowColor];
        _iamgeBtn.center = CGPointMake(self.bounds.size.height / 2, self.bounds.size.width / 2);
        

        //设置默认波纹颜色 
        self.raderColor = [UIColor blackColor];
    }
    return self;
}

- (void)dealloc{
    NSLog(@"radar dealloc");
}



//重写setter方法
-(void)setImage:(UIImage *)image
{
    _image = image;
    [self.iamgeBtn setImage:image forState:0];
}


//画雷达圆圈图
-(void)setUp
{
    NSLog(@"执行定时器方法");
    CGPoint center = CGPointMake(self.bounds.size.height / 2, self.bounds.size.width / 2);
    
    //使用贝塞尔画圆
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:10 startAngle:0 endAngle:2 * M_PI clockwise:YES];
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.bounds;
    //填充颜色
    shapeLayer.fillColor = self.raderColor.CGColor;
    //设置透明度、此处最好和动画里的一致、否则效果会不好
    shapeLayer.opacity = 0.2;
    shapeLayer.path = path.CGPath;
    
    
    [self.layer insertSublayer:shapeLayer below:self.iamgeBtn.layer];
    
    [self addAnimation:shapeLayer];
}

-(void)addAnimation:(CAShapeLayer *)shapeLayer
{
    //雷达圈圈大小的动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animation];
    basicAnimation.keyPath = @"path";
    CGPoint center = CGPointMake    (self.bounds.size.height / 2, self.bounds.size.width / 2);
    UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:center radius:5 startAngle:0 endAngle:2 * M_PI clockwise:YES];//1-45
    path1.lineWidth = 3;

//    [path1 fill];
    UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:210 startAngle:0 endAngle:2 * M_PI clockwise:YES];
//    [path2 fill];
    basicAnimation.fromValue = (__bridge id _Nullable)(path1.CGPath);
    basicAnimation.toValue = (__bridge id _Nullable)(path2.CGPath);
    //保持最新状态
    basicAnimation.fillMode = kCAFillModeForwards;
    
    
    //雷达圈圈的透明度
    CABasicAnimation *opacityAnimation = [CABasicAnimation animation];
    opacityAnimation.keyPath = @"opacity";
    
    opacityAnimation.fromValue = @(0.3);//0.2 - 0.5
    opacityAnimation.toValue = @(0);
    opacityAnimation.fillMode = kCAFillModeForwards;
    
    //组动画
    CAAnimationGroup *group = [CAAnimationGroup animation];
    group.animations = @[basicAnimation,opacityAnimation];
    group.duration = 7;
    //动画定时函数属性 先快后慢
    group.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    group.delegate = self;
    //指定的时间段完成后,动画就自动的从层上移除
    group.removedOnCompletion = YES;
    //添加动画到layer
    [shapeLayer addAnimation:group forKey:nil];
    
}

#pragma mark 动画结束
-(void)animationDidStop:(CAAnimation *)anim     finished:(BOOL)flag
{
    //虽然效果实现了、而且在内存上也基本在25m旁边.但是实现雷达波纹的波浪是一直在创建对象、所以下叙是为了及时释放不用的Shapelayear。
    //此种方法欠妥、后面有时间会做更新
    if (flag) {
//        NSLog(@"----");
        //释放动画结束的对象  获取最上层layer
        if ([self.layer.sublayers[0] isKindOfClass:[CAShapeLayer class]]) {
            CAShapeLayer *shaperLayer = (CAShapeLayer *)self.layer.sublayers[0];
            [shaperLayer removeFromSuperlayer];
            shaperLayer = nil;
//            NSLog(@"%lu",self.layer.sublayers.count);
        }
        
    }
}


@end

